<template>
	<gui-page ref="guiPage" :customHeader="true">
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('hardware.pageTitleEnter')"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="distributed">
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text" style="width:400rpx">{{ $t('hardware.receiptNo')
							}}</text>
						<view class="gui-form-body gui-text-right">
							<input type="text" class="gui-form-input gui-text-right" v-model="fromData.receiptCode"
								:placeholder="$t('distributedEnter.placeholderText')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text">{{ $t('distributedEnter.date') }}</text>
						<view class="gui-form-body gui-text-right" @click="dateChoose">
							<view class="gui-form-input gui-text-right" v-if="fromData.chooseTime.length">
								<text>{{ fromData.chooseTime[0] }}</text> {{ fromData.chooseTime[1] ? '-' : '' }}
								<text>{{ fromData.chooseTime[1] }}</text>
							</view>
							<input v-else type="button" class="gui-form-input gui-text-right"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text">{{ $t('smashRemoval.completionStatus') }}</text>
						<view class="gui-form-body gui-text-right" style="overflow:visible;">
							<superweiCombox style="text-align: right;" :candidates="data.selectList"
								:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="name"
								@select="select" v-model="fromData.selectIdText" :defaultValue="$t('smashRemoval.all')">
							</superweiCombox>
						</view>
					</view>
					<view class="gui-form-item gui-border-b gui-flex gui-justify-content-center">
						<view class="gui-flex gui-space-between gui-align-items-center"
							style="width: 70%;height: 100rpx;">
							<button type="default" class="gui-button gui-button-mini gui-bg-black4 gui-noborder"
								style="width:200rpx;" @click="reset">
								<text
									class="gui-block gui-color-black gui-button-text-mini gui-icons">{{ $t('distributedEnter.reset')
									}}</text>
							</button>
							<button type="default" class="gui-button gui-button-mini gui-bg-blue gui-noborder"
								style="width:200rpx;" @click="search(OPERATION_SEARCH)">
								<text
									class="gui-block gui-color-white gui-button-text-mini gui-icons">{{ $t('distributedEnter.search')
									}}</text>
							</button>
						</view>
					</view>
				</view>
				<!-- 底部显示区域 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolower">
					<view style="height: 15rpx"></view>
					<view class="card-boxCustom gui-transition-all" v-for="(x, i) in data.list" @click="goPage(x)"
						:key="i">
						<view class="card-head">
							<text>{{ $t('stockManage.receivingNotice') }}：{{ x.receiptCode }}</text>
							<gui-tags v-if="x.receiptStatus == 'complete'" :text="$t('translationImage.accomplish')"
								:customClass="['gui-transparent-bg', 'gui-color-blue']" :lineHeight="2.5" :size="12"
								borderColor="#008AFF"></gui-tags>
							<gui-tags v-else :text="$t('distributedEnter.statusNo')"
								:customClass="['gui-transparent-bg', 'gui-color-green']" :lineHeight="2.5" :size="12"
								borderColor="#39B55A"></gui-tags>
						</view>
						<view class="card-body" style="padding-bottom: 15rpx;">
							<view class="card-rotating" @click.stop="data.list[i].openBool = !data.list[i].openBool">
								<uni-icons v-if="!data.list[i].openBool" type="bottom" color="#9C9C9C"
									size="20"></uni-icons>
								<uni-icons v-if="data.list[i].openBool" type="top" color="#9C9C9C"
									size="20"></uni-icons>
							</view>
							<view class="card-body-item item">
								<text class="body-item-title">{{ $t('hardware.receiptNoDate') }}：</text>{{ x.receiptDate
								}}
								<gui-tags class="tags" v-if="x.giftQty > 0" :text="$t('taskList.giftQtys')"
									:customClass="['gui-transparent-bg', 'gui-color-red']" :lineHeight="2.5" :size="12"
									borderColor="red"></gui-tags>
							</view>
							<view class="card-body-item">
								<text class="body-item-title">{{ $t('stockManage.receivingQty') }}：</text> {{ x.totalQty
								}}
							</view>
							<view class="card-body-item">
								<text class="body-item-title">{{ $t('stockManage.residueReceivingQty') }}：</text>
								{{ x.waitQty }}
							</view>

							<!-- <view class="card-body-item">
								<text class="body-item-title">{{ $t('distributedEnter.compiler') }}：</text>
								{{ x.checkuserName }}
							</view> -->
						</view>
						<view class="card-body-folding" v-show="data.list[i].openBool">
							<openView :materialGroups="x.receiptDs"></openView>
						</view>
					</view>
					<view v-if="data.noticeHooks" class="noticeHooks">
						{{ data.list.length == 0 ? $t('stockManage.notAvailable') : $t('stockManage.without') }}
					</view>
				</scroll-view>
			</view>
			<calendarCustom ref="calendarCustomRef" @setTime="getTime" :selectTime="fromData.chooseTime">
			</calendarCustom>
		</template>
	</gui-page>
</template>
<script setup>
import openView from '@/components/common/openView';
import searchCustom from '@/components/searchCustom';
import superweiCombox from '@/components/superweiCombox';
import calendarCustom from '@/components/calendarCustom';
import { useStore } from "vuex";
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { findReceiptHs } from '@/api/api.js'
// 引入 
import { onShow } from '@dcloudio/uni-app';
import graceJS from "@/Grace6/js/grace.js";
import { navigate } from '@/utils/routerSkip.js';
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_SEARCH, OPERATION_REASCT, OPERATION_LOOK, OPERATION_CHOOSE } from '@/constant/index.js'
//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

components: {
	searchCustom,
		superweiCombox,
		calendarCustom,
		openView
}
let calendarCustomRef = ref(null);
let fromData = reactive({
	chooseTime: [],
	selectIdText: t('smashRemoval.all'),//选中文字
	selectId: '', //选中校验状态的id
	receiptCode: '',
	page: 1,
	rows: 10,
});
let store = useStore();
let data = reactive({
	selectList: [{
		id: 'noComplete',
		name: t('distributedEnter.statusNo')
	}, {
		id: 'complete',
		name: t('distributedEnter.statusOk')
	}, {
		id: '',
		name: t('smashRemoval.all')
	}],// 校验状态数组
	operationType: '',
	noticeHooks: false, //暂无相关数据标识
	list: []
});
const {
	ctx
} = getCurrentInstance();
onMounted(() => {

})
onShow(() => {
	getSystemTimeRequest()
})
const goPage = (x) => {
	if (x.receiptStatus == 'complete') {
		// t('JsInternational.receiptBill')
		uni.showToast({ title: t('materials.receiptNoticeCompleted'), icon: "none" });
		return
	}
	try {
		let inputParameter = x.receiptCode
		let returnParameter = '无'
		let operationPicture = '五金仓入库'
		let callInterface = '无'
		let opertionParmes = {
			inputParameter,
			returnParameter,
			operationPicture,
			callInterface,
			responseCode: '无',//返回状态
			errorMessage: '无',//错误信息
			isPass: 'Y',//是否成功 Y N
			operationType: OPERATION_CHOOSE,
			module: 'WMS',
			source: "PDA",
		}
		basPdaOperationLogFn(opertionParmes)
	} catch (error) {

	}
	navigate({
		url: `/pages/hardware/hardwareEnterDetail?id=${x.id}`
	})
}
const getSystemTimeRequest = () => {
	if (fromData.chooseTime.length > 0) {
		search(OPERATION_LOOK)
		return
	}
	store.dispatch('getSystemTimeRequest').then(res => {
		console.log(res, '----param-----')
		if (res.length) {
			let a = dataTime(res)
			fromData.chooseTime.push(a)
			fromData.chooseTime.push(res)
			search(OPERATION_LOOK)
		}
	})
}

// 进入页面默认日期
const dataTime = (a) => {
	const end = (new Date(a).getTime() / 1000) - (60 * 60 * 24 * 7)
	const lastDate = new Date(parseInt(end) * 1000).getFullYear()
	const month = String(new Date(parseInt(end) * 1000).getMonth() + 1).padStart(2, '0'); // 补零
	const day = String(new Date(parseInt(end) * 1000).getDate()).padStart(2, '0'); // 补零
	return `${lastDate}-${month}-${day}`;
}
// 校验状态的选择事件
const select = (e) => {
	fromData.selectId = e.id;
}
const search = (type) => {
	fromData.page = 1
	data.operationType = type
	getRequestData()
}
const scrolltolower = (e) => {
	if (data.noticeHooks) return
	fromData.page++
	getRequestData()
}
const getRequestData = () => {
	let start = ''
	let end = ''
	if (fromData.chooseTime.length) {
		start = fromData.chooseTime[0] + ' 00:00:00'
		end = (fromData.chooseTime[1] ? fromData.chooseTime[1] : (fromData.chooseTime[0] || '')) + ' 23:59:59'
	}
	let parmes = {
		IsSp: "1",//区分是五金来时来料
		current: fromData.page,
		size: fromData.rows,
		dateStart: start,
		dateEnd: end,
		receiptStatus: fromData.selectId,
		receiptCode: fromData.receiptCode,
		IsBigmaterial: "0"  // 是否大料
	}
	findReceiptHs(parmes).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '五金仓入库'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code,//返回状态
				errorMessage: res.msg,//错误信息
				isPass: res.code == 0 ? 'Y' : 'N',//是否成功 Y N
				operationType: data.operationType,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.code == 0) {
			data.noticeHooks = false
			if (fromData.page === 1) {
				if (res.data.records.length == 0) {
					data.noticeHooks = true
				}
				data.list = res.data.records
				data.list.forEach(element => {
					element.openBool = false
				});
			} else {
				let arr = res.data.records
				arr.forEach(element => {
					element.openBool = false
				});
				data.list = [...data.list, ...arr]
			}
			data.noticeHooks = data.list.length >= res.data.total
		}
	})
}
const reset = () => {
	data.operationType = OPERATION_REASCT
	fromData.chooseTime = []
	fromData.page = 1
	fromData.oddNum = ''
	fromData.receiptCode = ""
	data.noticeHooks = false
	fromData.selectId = ''
	fromData.selectIdText = ''
	getRequestData()
}
const dateChoose = () => {
	calendarCustomRef.value.open();
}
const getTime = (times) => {
	fromData.chooseTime = times;
}
</script>
<style lang="scss" scoped>
.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.noticeHooks {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 80rpx;
	font-size: 32rpx;
	color: #999;
}

.scroll {
	flex: 1;
	height: 2rpx;
}

.distributed {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.card-box {
	margin: 0 15rpx;
	box-shadow: 0rpx 0rpx 4rpx 0 #A5A5A5;
	background: #fff;
	padding: 30rpx;
	border-radius: 16rpx;
}

.item {
	position: relative;

	.tags {
		position: absolute;
		right: 0;
		top: 0;
	}
}

.card-item {
	line-height: 60rpx;
}

.card-bottom {
	margin-bottom: 30rpx;
}

.filtrate-item {
	display: flex;
	align-items: center;
	font-size: 32rpx;
	margin-bottom: 20rpx;

	.filtrate-itemLeft {
		height: 32rpx;
		line-height: 32rpx;
		width: 100rpx;
	}

	.filtrate-itemRight {
		border: 2rpx solid #A5A5A5;
		border-radius: 8rpx;
		overflow: hidden;
		line-height: 40rpx;
		width: 300rpx;
	}
}
</style>
